﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace cs2335lab3grp1
{
    /// <summary>
    /// A student manages an ID, a name, and a list of grades.
    /// It can calculate the average of those grades, and can
    /// output a human-readable string with relevant information.
    /// </summary>
    public class Student
    {
        #region constructors

        public Student()
            : this(0, "invalid name", null)
        {
            // default constructor
        }

        public Student(int id, string name)
            : this(id, name, null)
        {
            // two parameters
        }

        public Student(int id, string name, List<int> grades)
        {
            // assign passed parameters to variables
            this.id = id;
            this.name = name;
            this.grades = grades;
        }
        #endregion
        #region instance vars and properties
        private int id;
        [XmlAttribute]
        public int ID
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private List<int> grades;
        public List<int> Grades
        {
            get { return grades; }
            set { grades = value; }
        }
        #endregion
        #region methods
        /// <summary>
        /// Returns a string with the student's name & average.
        /// </summary>
        /// <returns>String with student's name & average.</returns>
        public override string ToString()
        {
            // print out the name and average
            string output = "";
            output += "Name: ";
            output += name;
            output += "\tAverage: ";
            output += Average();

            return output;
        }

        /// <summary>
        /// Returns the student's average.
        /// </summary>
        /// <returns>The student's average.</returns>
        public double Average()
        {
            // if there aren't any grades, return 0
            if (grades == null)
            {
                return 0;
            }

            // loop through grades, find sum and count
            double sum = 0;
            int count = 0;
            foreach (int grade in grades)
            {
                sum += (double)grade;
                count++;
            }

            // divide according to your preference, round
            double avg = Math.Round(sum / count, 2);
            return avg;
        }
        #endregion
    }
}